package com.travel.carmen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


/**
	Categories for the Travel Mobile Application Carmen.
	 The View is the Categories List View (layout as per categories.xml) for the application.
	 Provides users with a list of categories matching those available on the Carmen (Drupal)
	 Website. These category values are stored locally (values as per categories.xml) to 
	 eliminate the need for an XMLRPC request.
   	@author Arturo Aparicio 
   	@author Edwin Boykin 
   	@author Veena Chengalnaidu
   	@author Anton Ganeshalingam
   	@author Lakshmi Mallampati
   	@author Tarun Morton
   	@author Chantha Thoeun
*/
public class Categories extends Activity implements OnItemClickListener
{
	private ListView categories;
	private String username;
	private String password;
	private String caller;
	
	@Override
	/**
	   Called when the activity is first created.
	    The UI activities as per categories.xml are inflated.
	    All listeners and adapters are attached and all settings are
	    made to default.
	 */
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.categories);
		categories = (ListView) findViewById(R.id.list_of_categories);
		
		caller   = getIntent().getStringExtra(getString(R.string.caller));
		username = getIntent().getStringExtra(getString(R.string.username));
		password = getIntent().getStringExtra(getString(R.string.password));
		
		ArrayAdapter categoryAdapter = ArrayAdapter.createFromResource(this, 
				R.array.categories, android.R.layout.simple_list_item_1);
		categories.setAdapter(categoryAdapter);
		categories.setOnItemClickListener(this);
	}
	
	
	/**
	   List View Listener to start an activity depending on the user intent.
	    Category information is passed onto the next activity. These can be,
	    SavedNotes or DeleteNote.
	   @param arg0 the Adapter for this listener
	   @param arg1 the View for this listener
	   @param arg2 the position of the item (Category) selected
	   @param arg3 --
	 */
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
	{
		if (caller.equals(getString(R.string.query_saved_note)))
		{
			Intent savedNote = new Intent(this, SavedNotes.class);
			savedNote.putExtra(getString(R.string.username), username);
			savedNote.putExtra(getString(R.string.password), password);
			savedNote.putExtra(getString(R.string.select_category), 
					categories.getItemAtPosition(arg2).toString());
			savedNote.putExtra(getString(R.string.category_position), arg2);
			startActivityForResult(savedNote, R.string.query_saved_note);
		}
		
		if (caller.equals(getString(R.string.delete)))
		{
			Intent deleteNote = new Intent(this, DeleteNote.class);
			deleteNote.putExtra(getString(R.string.username), username);
			deleteNote.putExtra(getString(R.string.password), password);
			deleteNote.putExtra(getString(R.string.select_category), 
					categories.getItemAtPosition(arg2).toString());
			startActivityForResult(deleteNote, R.string.delete);
		}
	}
	
	
	/**
	   Returns to caller with the result from activities called by this.
	    Activity remains active if the activity called returned an empty list.
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
		if (resultCode == Main.EMPTY)
		{
			try
			{
				Toast.makeText(getApplicationContext(), 
					"You do not have any notes under \n" + 
					data.getStringExtra(Main.BLANK), 1).show();
			}
			catch (Exception ex)
			{ /* Do nothing user pressed back/cancel */}
		}
		else
		{
			setResult(resultCode, data);
			finish();
		}
    }
}
